package com.tebiecloud.store.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tebiecloud.store.client.model.entity.StoreFlow;
import com.tebiecloud.store.client.model.vo.StoreFlowList;
import com.tebiecloud.store.server.service.StoreFlowService;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 门店流水 前端控制器
 * @author tebie
 * @date 2019-09-15
 */
@RestController
@RequestMapping("/storeflow")
public class StoreFlowController {

    @Autowired
    private StoreFlowService storeFlowService;

    /**
     * 获取门店流水
     * @param page
     * @param limit
     * @return
     */
    @PostMapping(value = "list")
    public ResultBody<IPage<StoreFlowList>> list(
            @RequestParam(value = "page", defaultValue = "1", required = false) Integer page,
            @RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit,
            @RequestParam(value = "storeId") long storeId
    ) {
        QueryWrapper<StoreFlow> storeFlowQueryWrapper = new QueryWrapper<>();
        storeFlowQueryWrapper.select("store_id","SUM(amount) AS amount","type","type_id","desp","year","month","day");
        storeFlowQueryWrapper.eq("store_id",storeId);
        storeFlowQueryWrapper.groupBy("year","month","day","type");
        storeFlowQueryWrapper.orderByDesc("year","month","day");
        IPage<StoreFlow> storeFlowIPage = storeFlowService.page(new Page<StoreFlow>(page,limit),storeFlowQueryWrapper);
        List<StoreFlow> storeFlowList1 = storeFlowIPage.getRecords();
        IPage<StoreFlowList> storeFlowListIPage = new Page<>();
        List<StoreFlowList> StoreFlowListList = new ArrayList<>();
        storeFlowListIPage.setCurrent(page);
        storeFlowListIPage.setTotal(storeFlowIPage.getTotal());
        storeFlowListIPage.setSize(storeFlowIPage.getSize());
        storeFlowListIPage.setPages(storeFlowIPage.getPages());
        if(storeFlowList1!=null && storeFlowList1.size()>0){
            for(StoreFlow storeFlow:storeFlowList1){
                StoreFlowList storeFlowList = new StoreFlowList();
                storeFlowList.setAmount(storeFlow.getAmount());
                storeFlowList.setDay(storeFlow.getDay());
                storeFlowList.setMonth(storeFlow.getMonth());
                storeFlowList.setYear(storeFlow.getYear());
                storeFlowList.setTypeId(storeFlow.getTypeId());
                storeFlowList.setType(storeFlow.getType());
                storeFlowList.setDesp(storeFlow.getDesp());
                storeFlowList.setDayMonth(storeFlow.getMonth()+"月"+storeFlow.getDay()+"日");
                storeFlowList.setDayMonthYear(storeFlow.getYear()+"-"+storeFlow.getMonth()+"-"+storeFlow.getDay());
                storeFlowList.setStoreId(storeId);
                StoreFlowListList.add(storeFlowList);
            }
        }
        storeFlowListIPage.setRecords(StoreFlowListList);
        return ResultBodyUtils.ok(storeFlowListIPage);
    }

}
